/*
 * DeliveryPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.agents.delivery;

import android.text.TextUtils;

import com.yidiandao.business.data.model.AgentModel;
import com.yidiandao.business.data.model.IAgentModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 配送订单-Presenter
 */
public class DeliveryPresenter implements DeliveryContract.Presenter {

    private final IAgentModel mAgentModel;
    private DeliveryContract.View mView;
    private boolean isLoading;

    public DeliveryPresenter(DeliveryContract.View view) {
        mView = view;
        mView.setPresenter(this);
        mAgentModel = new AgentModel();
    }

    @Override
    public void start() {
        getNetData(true);
    }

    @Override
    public void getNetData(final boolean isRefresh) {
        mAgentModel.loadDeliveryList(
                isRefresh,
                mView.getPagerTag(),
                new IAgentModel.OnGetAgentListener() {
                    @Override
                    public void onSuccess(
                            List<DisplayListItem> displayListItems,
                            boolean empty,
                            boolean hasMore) {
                        if (isRefresh) {
                            mView.refreshContent(mView.checkResultObject(displayListItems));
                            mView.setRefreshComplete();
                        }
                        mView.loadMoreFinish(empty, hasMore);
                        mView.setDisplayListItems(displayListItems);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefresh) {
                            mView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void handleEvent(int type, String orderId) {
        if (isLoading) return;
        isLoading = true;
        mView.showDialog();
        mAgentModel.handleEventFromDelivery(
                mView.getPagerTag(),
                type,
                orderId,
                new IAgentModel.OnLoadNetListener() {
                    @Override
                    public void onSuccess(String tip) {
                        if (!TextUtils.isEmpty(tip)) UIUtils.showToast(tip);
                        start();
                    }

                    @Override
                    public void onFailed() {
                    }

                    @Override
                    public void onFinal() {
                        isLoading = false;
                        mView.dismissDialog();
                    }
                });
    }
}
